
package com.scjyzr.mysweeper;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class GameActivity extends Activity implements MyCallBack {

    private GameView gameView;

    private TextView time;

    private CheckBox check;

    private MyTimer myTimer = null;

    private Handler myHandler = null;

    private final int MSG = 11;

    private Message msg = null;

    private ImageButton refreshBut = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.game_layout);
        

        RelativeLayout info = (RelativeLayout) findViewById(R.id.info);
        info.setBackgroundColor(Color.GRAY);

        refreshBut = (ImageButton) findViewById(R.id.restart);

        gameView = (GameView) findViewById(R.id.gameViewId);
        gameView.setMyParentCallBack(this);
        time = (TextView) findViewById(R.id.time);
        check = (CheckBox) findViewById(R.id.checkFalg);

        gameView.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
               
                if (gameView.onClick(event.getX(), event.getY(), check.isChecked(), time.getText().toString())) {

                    check.setChecked(false);
                }                
                

                return false;
            }

        });
        initializeHander();

        myTimer = new MyTimer(this);

        refreshBut.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                gameView.restartGame();
                time.setText(msg.arg1 + "");
                myTimer.startTimer();

                v.setVisibility(View.INVISIBLE);
                StatisiticUtil.functiontSatistics(GameActivity.this, StatisiticUtil.StatisticKey.FRESH_GAME);

            }
        });

    }

    @Override
    public void onResume() {
        super.onResume();
        myTimer.startTimer();
        StatisiticUtil.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        myTimer.onPause();
        StatisiticUtil.onPause(this);
    }
    
    @Override
    protected void onDestroy() {
        if (gameView != null) {
            gameView.clearVar();
            gameView = null;
        }

        time = null;
        check = null;
        if (myTimer != null) {
            myTimer.destory();
        }
        myTimer = null;

        if (myHandler != null) {
            myHandler.removeMessages(MSG);
            myHandler = null;
        }
        super.onDestroy();
    }

    @Override
    public void myCallBack(Object object, int flag) {
        if (flag == MyCallBack.IS_TIME) {
            // 这个是定时器返回的值。
            if (object != null) {

                msg = new Message();
                msg.what = MSG;
                msg.arg1 = Integer.valueOf(object + "");
                myHandler.sendMessage(msg);
            }
        } else if (flag == MyCallBack.IS_GAME_OVER) {
            myTimer.stopTimer();
            time.setText("");
            myTimer.startTimer();
            StatisiticUtil.functiontSatistics(this, StatisiticUtil.StatisticKey.GAME_OVER);

        } else if (flag == MyCallBack.IS_WIN) {
            myTimer.stopTimer();
            refreshBut.setVisibility(View.VISIBLE);
            StatisiticUtil.functiontSatistics(this, StatisiticUtil.StatisticKey.WIN_GAME);

        }

    }

    private void initializeHander() {
        myHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == MSG) {
                    if (time != null) {
                        time.setText(msg.arg1 + "");
                    }
                }
            }
        };
    }
}
